package pingan.com.imandtwo.presenter;

import android.util.Log;

import java.util.HashMap;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import pingan.com.imandtwo.beans.BannerBean;
import pingan.com.imandtwo.beans.IsRegisterBean;
import pingan.com.imandtwo.beans.LoginBean;
import pingan.com.imandtwo.beans.ValidateCodeBean;
import pingan.com.imandtwo.interfaces.Iview;
import pingan.com.imandtwo.module.HttpModel;

/**
 * Created by xsj on 2018/2/5.
 */

public class HttpPresenter {

    private Iview iv;
    private DisposableSubscriber<BannerBean> p1;
    private DisposableSubscriber<LoginBean> login;
    private DisposableSubscriber<ValidateCodeBean> registerview;
    private DisposableSubscriber<IsRegisterBean> isRegister;
    private DisposableSubscriber<ValidateCodeBean> upDatapsd;
    private DisposableSubscriber<ValidateCodeBean> yanzhenma;
    private DisposableSubscriber<ValidateCodeBean> addfriend;

    public HttpPresenter(Iview iv) {
        this.iv = iv;
    }

    //获取传过来的map
    public void getMap(HashMap<String,String> map,String tag){
        HttpModel httpModel = new HttpModel(this);
        httpModel.getData(map,tag);
    }


    //轮播图数据
    public void getLunBoData(Flowable<BannerBean> flowable, final String tag) {
        p1 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<BannerBean>() {
                    @Override
                    public void onNext(BannerBean bannerBean) {
                        iv.onSuccess(bannerBean,tag);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getLoginData(Flowable<LoginBean> flowable, final String tag) {
        if(tag.equals("登录")){
            login = flowable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<LoginBean>() {
                        @Override
                        public void onNext(LoginBean loginBean) {
                            Log.e("TAG","请求成功:"+loginBean.toString());
                            iv.onSuccess(loginBean, tag);
                        }

                        @Override
                        public void onError(Throwable t) {
                            Log.e("TAG","请求失败:"+t.getMessage().toString());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    public void getZhuCeData(Flowable<ValidateCodeBean> zhuCeData, final String tag) {
        if(tag.equals("注册")){
            registerview = zhuCeData.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<ValidateCodeBean>() {
                        @Override
                        public void onNext(ValidateCodeBean validateCodeBean) {
                            iv.onSuccess(validateCodeBean, tag);
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        if(tag.equals("添加好友")){
            addfriend = zhuCeData.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<ValidateCodeBean>() {
                        @Override
                        public void onNext(ValidateCodeBean validateCodeBean) {
                            iv.onSuccess(validateCodeBean,tag);
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    public void getIsRegister(Flowable<IsRegisterBean> flowable, final String tag) {
        if(tag.equals("是否注册")){
            isRegister = flowable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<IsRegisterBean>() {
                        @Override
                        public void onNext(IsRegisterBean isRegisterBean) {
                            iv.onSuccess(isRegisterBean, tag);
                        }

                        @Override
                        public void onError(Throwable t) {
                            Log.e("TAG","请求失败:"+t.getMessage().toString());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    public void getPhoneNumber(Flowable<ValidateCodeBean> flowable, final String tag){
        if(tag.equals("注册验证码")){
            yanzhenma = flowable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<ValidateCodeBean>() {
                        @Override
                        public void onNext(ValidateCodeBean validateCodeBean) {
                            iv.onSuccess(validateCodeBean, tag);
                        }

                        @Override
                        public void onError(Throwable t) {
                            Log.e("TAG", "请求失败:" + t.getMessage());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    public void getUpdatepsd(Flowable<ValidateCodeBean> flowable,final String tag) {
        if(tag.equals("修改密码")){
            upDatapsd = flowable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSubscriber<ValidateCodeBean>() {
                        @Override
                        public void onNext(ValidateCodeBean validateCodeBean) {
                            iv.onSuccess(validateCodeBean, tag);
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }


    //防止泄露
    public void disAttach(){

        if(p1!=null){
            if(p1.isDisposed()){
                p1.dispose();
            }
        }

        if(login!=null){
            if(login.isDisposed()){
                login.dispose();
            }
        }

        if(registerview!=null){
            if(registerview.isDisposed()){
                registerview.dispose();
            }
        }

        if(isRegister!=null){
            if(isRegister.isDisposed()){
                isRegister.dispose();
            }
        }

        if(yanzhenma!=null){
            if(yanzhenma.isDisposed()){
                yanzhenma.dispose();
            }
        }

        if(upDatapsd!=null){
            if(upDatapsd.isDisposed()){
                upDatapsd.dispose();
            }
        }

        if(addfriend!=null){
            if(addfriend.isDisposed()){
                addfriend.dispose();
            }
        }
    }

}
